package com.link.base.base.product.controller;

import com.link.base.base.position.model.Position;
import com.link.base.base.product.model.ProdQuote;
import com.link.base.base.product.service.ProdQuoteService;
import com.link.core.basic.annotations.JsonParam;
import com.link.core.basic.controller.BasicController;
import com.link.core.basic.model.BasicModel;
import com.link.core.basic.service.BasicService;
import com.link.core.basic.service.KeyGenerateService;
import com.link.core.cllog.LCLogger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 零件报价表
 *
 * @author 阳晓琳
 * @date 2020-10-16 17:19:06
 */
@Controller
@RequestMapping("/link/prodQuote")
public class ProdQuoteController extends BasicController<ProdQuote> {

    @Resource
    private KeyGenerateService keyGenerateService;

    @Resource
    private ProdQuoteService prodQuoteService;

    @Override
    public BasicService<ProdQuote> getBasicService() throws Exception {
        return prodQuoteService;
    }

    /**
     *总成报价 根据预设利率计算
     *
     * @author 王昊
     * @date 2020/11/12
     */
    @RequestMapping(value = "/presetRateCalcul", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> presetRateCalcul(@JsonParam ProdQuote entity, HttpSession session, HttpServletRequest request) {
        Map<String, Object> result = new HashMap<String, Object>(4);
        try {
            prodQuoteService.presetRateCalcul(entity);
            result.put("newRow",entity);
            result.put("success", true);
        } catch (Exception e) {
            result.put("success", false);
            result.put("result", e.getMessage());
        }
        return result;
    }

    /**
     * 承接单产品行，根据项目ID，项目最新报价轮次筛选 总成 pick
     * 通用picklist
     */
    @RequestMapping(value = "/partPicklist", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> picklist(ProdQuote qps, HttpSession session, HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> result = new HashMap<String, Object>();
        try {
            //设置安全性
            List<ProdQuote> prodQuotes = prodQuoteService.queryAllByQuote(qps);
            result.put("success", true);
            result.put("rows", prodQuotes);
            result.put("total", getCount(((BasicModel) qps), prodQuotes.size()));
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("rows", new ArrayList<Position>());
            result.put("result", e.getMessage());
        }
        return result;
    }
}